package com.jse.template.freemarker;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateException;

public class FreeMarkerUtil {
	
	Configuration configuration;
	public FreeMarkerUtil() {
		Configuration configuration = Configuration.getDefaultConfiguration();
        
        //构建  \WEB-INF\ftl 的文件路径  
        String path = FreeMarkerUtil.class.getResource("/").getPath();  
        path = path.substring(1, path.indexOf("classes"));  
        File ftlPathDir = new File(path+File.separator+"ftl");  
          
        try {  
            configuration.setDirectoryForTemplateLoading(ftlPathDir);  
            configuration.setObjectWrapper(new DefaultObjectWrapper());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
	}
      
    public void xx(String template,HttpServletRequest req,HttpServletResponse resp) throws  IOException {
    	req.getAttributeNames();
    	try {
    		configuration.getTemplate(template).process(1, resp.getWriter());  
		} catch (TemplateException e) {
			// TODO: handle exception
		}
    	
    }
}
